<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template v-slot:start_end_time="{row}">
      <div class="flex" style="flex-direction: column">
        <p>{{row.start_time}}</p>
        <p>{{row.end_time}}</p>
      </div>
    </template>
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('Promotion/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('Promotion/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('Promotion/add')) btns['添加'] = function(){ iframe('promotion/edit') }
      if(in_right_admin('Promotion/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('Promotion/recycle')) btns['回收站'] = function(){ iframe('promotion/index?is_del=1') }
    }
    let time_stage_map = ['<span>未开始</span>','<span style="color: var(--ok)">进行中</span>','<span style="color: var(--fail)">已过期</span>'];
    let model_name_config = decode_config(_config('model_map'), {content_key:'label', value_key:'name'})
    let forms_config = {};
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      title:{name:'title', label:'标题', type:'text_one', width:'100px'},
      time_stage:{name:'time_stage', label:'状态', width:'60px', type:'select_one', config:time_stage_map},
      start_end_time:{name:'start_end_time', label:'活动时间', width:'165px', type:'start_end_time'},
      model_name: {label:'限制类型',name:'model_name',type:'select_one',config:model_name_config, width:'80px'},
      limit_ids: {label:'限制项目ID集',name:'limit_ids', min_width:'120px'},
      user_ids:{name:'user_ids', label:'限制用户', min_width:'120px'},
      level:{name:'level', label:'限制等级', type:'checkbox', min_width:'120px'},
      min_pay_money:{name:'min_pay_money', label:'满足金额', width:'75px'},
      max_use: {name:'max_use',label:'次数限制', width:'75px'},
      max_buy: {name:'max_buy',label:'仅首次购买',type:'radio_switch', width:'75px'},
      reduction:{name:'reduction', label:'减免内容', width:'140px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/promotion/'+(is_del?'recycle':'list'),
        title: "促销活动列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          let reduction_type_map = ['减免 {value} 元', '折扣 {value}%', '项目价格降低至 {value} 元'];
          let now_date = s94Mjs.date('Y-M-D H:I:S');
          s94Mjs.each(list, function(v){
            v.time_stage = 0;
            if (now_date > v.start_time) {
              v.time_stage++;
              if (now_date > v.end_time) v.time_stage++;
            }
            v.reduction = reduction_type_map[v.reduction_type].replace('{value}', v.reduction_value);
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('Promotion/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('promotion/edit?'+s94Mjs.param_encode(param))
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/user_level/list', cache:true}, (res)=>{
      let level_config = decode_config(res.data.list, {content_key:'name', value_key:'level'});
      app.config.list_cols.level.config = level_config;
      return false
    })
  }
}
</script>
